<template>
	<view class="forget">
		
		<view class="content">
			<!-- 主体 -->
			<view class="main">
				<view class="tips">若你忘记了密码，可在此重置新密码。</view>
				<wInput
					v-model="mobile"
					type="text"
					maxlength="11"
					placeholder="请输入手机号码"
				></wInput>
				<wInput
					v-model="password"
					type="password"
					maxlength="11"
					placeholder="请输入新密码"
					isShowPass
				></wInput>
				
				<wInput
					v-model="verCode"
					type="number"
					maxlength="6"
					placeholder="验证码"
					
					isShowCode
					codeText="获取重置码"
					setTime="300"
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
			</view>
			
			<wButton 
				text="重置密码"
				:rotate="isRotate" 
				@click.native="startRePass()"
			></wButton>

		</view>
	</view>
</template>

<script>
	var that;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	export default {
		data() {
			return {
				mobile: "", //手机号码
				password: "", //新密码
				verCode:"", //验证码
				isRotate: false, //是否加载旋转
			}
		},
		components:{
			wInput,
			wButton
		},
		mounted() {
			that= this;
		},
		methods: {
			getVerCode(){
				//获取验证码
				if (that.mobile.length != 11) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
				
				setTimeout(function(){
					that.$refs.runCode.$emit('runCode',0); //假装模拟下需要 终止倒计时
				},300000)
				//发送短信验证码
				that.sendSmsCode()
			},
			//发送短信验证码
			sendSmsCode(){
				let req={
					params:{
						mobile:that.mobile,
						templateId:'10711',
						type:'login',
					},
					active:'sendSmsCode',
					showLoad:false
				}
				that.GLOBAL.doCloud('user-center',req).then(res=>{
					if(res.code==0){
						uni.showToast({
							title: '发送验证码成功'
						});
					}
				})
			},
			startRePass() {
				//重置密码
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.mobile.length != 11) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
			    if (this.password.length < 6) {
			        uni.showToast({
			            icon: 'none',
						position: 'bottom',
			            title: '密码不正确'
			        });
			        return false;
			    }
				if (this.verCode.length != 6) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不正确'
				    });
				    return false;
				}
				that.forgetPassword()
			},
			forgetPassword(){
				//由于uni-id中没有忘记密码功能，我的思路【先手机号验证码直接登录--->重置密码】
				let req={
					params:{
						mobile:that.mobile,
						code:that.verCode,
						password:that.password,
						type:'login',
					},
					active:'forgetPassword',
					showLoad:false
				}
				that.GLOBAL.doCloud('user-center',req).then(res=>{
					if(res.code==0){
						uni.navigateBack({
							delta: 1
						});
						setTimeout(()=>{
							uni.showToast({
								title: '密码重置成功'
							});
						},1000)
					}
				})
			}
			
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>

